package com.example.phoenixshop.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.alibaba.fastjson.JSONObject;
import com.example.phoenixshop.bean.User;
import com.example.phoenixshop.service.UserService;
import com.example.phoenixshop.utils.BaseException;
import com.example.phoenixshop.utils.BaseResponse;
import com.example.phoenixshop.utils.ResponseCode;
import com.example.phoenixshop.utils.ResponseResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.Date;

@RestController
@BaseResponse
public class UserController {

    @Resource
    private UserService userService;

//    /**
//     * 通过账号名称查询用户信息
//     * @param account
//     * @return
//     */
//    @RequestMapping(method = RequestMethod.GET,value = "/users/{account}" )
//    @ResponseBody
//    public User getUser(@PathVariable(value = "account") String account){
//        User user =  userService.getUserInfoByAccountWithoutPwd(account);
//        if(user==null){
//            throw new BaseException(ResponseCode.RESOURCES_NOT_EXIST);
//        }else{
//            return user;
//        }
//    }

    /**
     *用户登录,data中是由用户id生成的TokenValue,success时成功
     * @param str 发送account和password，如{"account":"weiyyy","password":"123456"}
     * @return
     */
    @RequestMapping(method = RequestMethod.POST,value = "/login")
    public ResponseResult login(@RequestBody String str){
        JSONObject jsonArray = null;

        try {
            jsonArray = JSONObject.parseObject(str);
//            System.out.println(str);
            String account = jsonArray.getString("account");
            String password = jsonArray.getString("password");
            User user = userService.login(account,password);
            if(user.equals(null)){
                throw new BaseException(ResponseCode.RESOURCES_NOT_EXIST);
            }else {
                StpUtil.login(user.getId());
                return new ResponseResult(10000,"success",StpUtil.getTokenValue());
            }
        }catch (Exception e){
            return new ResponseResult(50000,"服务器异常",null);
        }
//        return new ResponseResult(10000,"success",StpUtil.getLoginIdByToken(""));
    }

    /**
     *用户注册，ResponseResult中状态success时成功
     * @param str 发送account,password，username如{"account":"weixxx","password":"123456","username":"张三"}
     * @return
     */
    @RequestMapping(method = RequestMethod.POST,value = "/register")
    public ResponseResult register(@RequestBody String str){
        JSONObject jsonArray = null;

        try {
            jsonArray = JSONObject.parseObject(str);
//            System.out.println(str);
            User user = new User();
            user.setAccount(jsonArray.getString("account"));
            user.setPassword(jsonArray.getString("password"));
            user.setRole(Byte.valueOf("0"));
            user.setRegisterTime(new Date());
            user.setUserName(jsonArray.getString("username"));
            int i = 0;
            i = userService.register(user);
            if(i==0){
                return new ResponseResult(40030,"重复插入",null);
            }else if(i==-1){
                throw new Exception();
            }else{
                return new ResponseResult(10000,"success",null);
            }
        }catch (Exception e){
            return new ResponseResult(50000,"服务器异常",null);
        }
//        return new ResponseResult(10000,"success",StpUtil.getLoginIdByToken(""));
    }
}
